package com.byx.chatclient.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.res.ResourcesCompat;

import com.byx.chatclient.R;

/**
 * SearchView
 *
 * @author Superb
 * @date 2021/4/23 23:56
 */
public class SearchView extends androidx.appcompat.widget.AppCompatEditText {
    private float searchSize = 0;
    private float textSize = 0;
    private int textColor = 0xFF000000;
    private String text = "搜索";
    private Drawable mDrawable;
    private Drawable mIcon;
    private Paint paint;

    private static final int DEFAULT_DRAWABLE = R.drawable.ic_search_black_24dp;

    public SearchView(@NonNull Context context) {
        super(context);
        initResource(context, null);
        initPaint();
    }

    public SearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initResource(context, attrs);
        initPaint();
    }

    public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initResource(context, attrs);
        initPaint();
    }

    private void initResource(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        float density = context.getResources().getDisplayMetrics().density;
        searchSize = mTypedArray.getDimension(R.styleable.SearchView_imageWidth, 18 * density + 0.5F);
        textColor = mTypedArray.getColor(R.styleable.SearchView_innerTextColor, 0xFF848484);
        textSize = mTypedArray.getDimension(R.styleable.SearchView_textSize, 14 * density + 0.5F);
        mIcon = mTypedArray.getDrawable(R.styleable.SearchView_icon);
        if (mIcon == null){
            mIcon = AppCompatResources.getDrawable(context,DEFAULT_DRAWABLE);
        }
        if (mTypedArray.hasValue(R.styleable.SearchView_innerText)){
            text = mTypedArray.getText(R.styleable.SearchView_innerText).toString();
        }
        mTypedArray.recycle();
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSearchIcon(canvas);
    }

    private void drawSearchIcon(Canvas canvas) {
        if (this.getText().toString().length() == 0) {
            float textWidth = paint.measureText(text);
            float textHeight = getFontLeading(paint);

            float dx = (getWidth() - searchSize - textWidth - 8) / 2;
            float dy = (getHeight() - searchSize) / 2;

            canvas.save();
            canvas.translate(getScrollX() + dx, getScrollY() + dy);
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
            canvas.drawText(text, getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint);
            canvas.restore();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mDrawable == null) {
            try {
                mDrawable = mIcon.getCurrent();
                mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize);
            } catch (Exception e) {

            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mDrawable != null) {
            mDrawable.setCallback(null);
            mDrawable = null;
        }
        super.onDetachedFromWindow();
    }

    public float getFontLeading(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.bottom - fm.top;
    }
}
